from ctypes import *

bd_stream_type_e = c_int
BLURAY_STREAM_TYPE_VIDEO_MPEG1              = 0x01
BLURAY_STREAM_TYPE_VIDEO_MPEG2              = 0x02
BLURAY_STREAM_TYPE_AUDIO_MPEG1              = 0x03
BLURAY_STREAM_TYPE_AUDIO_MPEG2              = 0x04
BLURAY_STREAM_TYPE_AUDIO_LPCM               = 0x80
BLURAY_STREAM_TYPE_AUDIO_AC3                = 0x81
BLURAY_STREAM_TYPE_AUDIO_DTS                = 0x82
BLURAY_STREAM_TYPE_AUDIO_TRUHD              = 0x83
BLURAY_STREAM_TYPE_AUDIO_AC3PLUS            = 0x84
BLURAY_STREAM_TYPE_AUDIO_DTSHD              = 0x85
BLURAY_STREAM_TYPE_AUDIO_DTSHD_MASTER       = 0x86
BLURAY_STREAM_TYPE_VIDEO_VC1                = 0xea
BLURAY_STREAM_TYPE_VIDEO_H264               = 0x1b
BLURAY_STREAM_TYPE_SUB_PG                   = 0x90
BLURAY_STREAM_TYPE_SUB_IG                   = 0x91
BLURAY_STREAM_TYPE_SUB_TEXT                 = 0x92
BLURAY_STREAM_TYPE_AUDIO_AC3PLUS_SECONDARY  = 0xa1
BLURAY_STREAM_TYPE_AUDIO_DTSHD_SECONDARY    = 0xa2

bd_video_format_e = c_int
BLURAY_VIDEO_FORMAT_480I              = 1  # ITU-R BT.601-5
BLURAY_VIDEO_FORMAT_576I              = 2  # ITU-R BT.601-4
BLURAY_VIDEO_FORMAT_480P              = 3  # SMPTE 293M
BLURAY_VIDEO_FORMAT_1080I             = 4  # SMPTE 274M
BLURAY_VIDEO_FORMAT_720P              = 5  # SMPTE 296M
BLURAY_VIDEO_FORMAT_1080P             = 6  # SMPTE 274M
BLURAY_VIDEO_FORMAT_576P              = 7  # ITU-R BT.1358

bd_video_rate_e = c_int
BLURAY_VIDEO_RATE_24000_1001          = 1  # 23.976
BLURAY_VIDEO_RATE_24                  = 2
BLURAY_VIDEO_RATE_25                  = 3
BLURAY_VIDEO_RATE_30000_1001          = 4  # 29.97
BLURAY_VIDEO_RATE_50                  = 6
BLURAY_VIDEO_RATE_60000_1001          = 7   # 59.94

bd_video_aspect_e = c_int
BLURAY_ASPECT_RATIO_4_3               = 2
BLURAY_ASPECT_RATIO_16_9              = 3

bd_audio_format_e = c_int
BLURAY_AUDIO_FORMAT_MONO              = 1
BLURAY_AUDIO_FORMAT_STEREO            = 3
BLURAY_AUDIO_FORMAT_MULTI_CHAN        = 6
BLURAY_AUDIO_FORMAT_COMBO             = 12  # Stereo ac3/dts, 

bd_audio_rate_e = c_int
BLURAY_AUDIO_RATE_48                  = 1
BLURAY_AUDIO_RATE_96                  = 4
BLURAY_AUDIO_RATE_192                 = 5
BLURAY_AUDIO_RATE_192_COMBO           = 12 # 48 or 96 ac3/dts
BLURAY_AUDIO_RATE_96_COMBO            = 14  # 48 ac3/dts

bd_char_code_e = c_int
BLURAY_TEXT_CHAR_CODE_UTF8            = 0x01
BLURAY_TEXT_CHAR_CODE_UTF16BE         = 0x02
BLURAY_TEXT_CHAR_CODE_SHIFT_JIS       = 0x03
BLURAY_TEXT_CHAR_CODE_EUC_KR          = 0x04
BLURAY_TEXT_CHAR_CODE_GB18030_20001   = 0x05
BLURAY_TEXT_CHAR_CODE_CN_GB           = 0x06
BLURAY_TEXT_CHAR_CODE_BIG5            = 0x07

bd_still_mode_e = c_int
BLURAY_STILL_NONE     = 0x00
BLURAY_STILL_TIME     = 0x01
BLURAY_STILL_INFINITE = 0x02


class BLURAY_STREAM_INFO(Structure):
    _fields_ = [
        ("coding_type", c_ubyte),
        ("format", c_ubyte),
        ("rate", c_ubyte),
        ("char_code", c_ubyte),
        ("lang", c_ubyte * 4),
        ("pid", c_ushort),
        ("aspect", c_ubyte)]

class BLURAY_CLIP_INFO(Structure):
    _fields_ = [
        ("pkt_count", c_uint),
        ("still_mode", c_ubyte),
        ("still_time", c_ushort),
        ("video_stream_count", c_ubyte),
        ("audio_stream_count", c_ubyte),
        ("pg_stream_count", c_ubyte),
        ("ig_stream_count", c_ubyte),
        ("sec_audio_stream_count", c_ubyte),
        ("sec_video_stream_count", c_ubyte),
        ("video_streams", POINTER(BLURAY_STREAM_INFO)),
        ("audio_streams", POINTER(BLURAY_STREAM_INFO)),
        ("pg_streams", POINTER(BLURAY_STREAM_INFO)),
        ("ig_streams", POINTER(BLURAY_STREAM_INFO)),
        ("sec_audio_streams", POINTER(BLURAY_STREAM_INFO)),
        ("sec_video_streams", POINTER(BLURAY_STREAM_INFO))]

class BLURAY_TITLE_CHAPTER(Structure):
    _fields_ = [
        ("idx", c_uint),
        ("start", c_ulong),
        ("duration", c_ulong),
        ("offset", c_ulong)]

class BLURAY_TITLE_INFO(Structure):
    _fields_ = [
        ("idx", c_uint),
        ("playlist", c_uint),
        ("duration", c_ulong),
        ("clip_count", c_uint),
        ("angle_count", c_ubyte),
        ("chapter_count", c_uint),
        ("clips", POINTER(BLURAY_CLIP_INFO)),
        ("chapters", POINTER(BLURAY_TITLE_CHAPTER))]

class BLURAY_SOUND_EFFECT(Structure):
    _fields_ = [
        ("num_channels", c_ubyte),
        ("num_frames", c_uint),
        ("samples", POINTER(c_ushort))]

class BLURAY_DISC_INFO(Structure):
    _fields_ = [
        ("bluray_detected", c_ubyte),
        ("first_play_supported", c_ubyte),
        ("top_menu_supported", c_ubyte),
        ("num_hdmv_titles", c_uint),
        ("num_bdj_titles", c_uint),
        ("num_unsupported_titles", c_uint),
        ("aacs_detected", c_ubyte),
        ("libaacs_detected", c_ubyte),
        ("aacs_handled", c_ubyte),
        ("bdplus_detected", c_ubyte),
        ("libbdplus_detected", c_ubyte),
        ("bdplus_handled", c_ubyte)]

BLURAY = c_void_p


libbluray = cdll.libbluray
libbluray.bd_get_titles.argtypes = [c_void_p, c_ubyte, c_uint]
libbluray.bd_get_titles.restype = c_uint
libbluray.bd_get_title_info.argtypes = [c_void_p, c_uint, c_uint]
libbluray.bd_get_title_info.restype = POINTER(BLURAY_TITLE_INFO)
libbluray.bd_get_playlist_info.argtypes = [c_void_p, c_uint, c_uint]
libbluray.bd_get_playlist_info.restype = POINTER(BLURAY_TITLE_INFO)
libbluray.bd_free_title_info.argtypes = [POINTER(BLURAY_TITLE_INFO)]
libbluray.bd_open.argtypes = [c_char_p, c_char_p]
libbluray.bd_open.restype = c_void_p
libbluray.bd_close.argtypes = [c_void_p]
libbluray.bd_get_disc_info.argtypes = [c_void_p]
libbluray.bd_get_disc_info.restype = POINTER(BLURAY_DISC_INFO)
